package com.khoayang.littlefighterchaos.menu;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.transitions.CCRotoZoomTransition;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import android.content.Context;
import com.khoayang.littlefighterchaos.game.GameScene;

public class ChooseCharacterLayer extends CCLayer {
	private BoxImageCharacter _boxImageCharacters[];
	private static String _characters[];
	private static int _teamIDs[];
	private Context _context;

	
	public ChooseCharacterLayer(Context context)
	{
		_context = context;
		_boxImageCharacters= new BoxImageCharacter[8];
		_characters = new String[8];
		_teamIDs = new int[8];
		CGPoint position;
		CGSize winSize = CCDirector.sharedDirector().winSize();
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<4;j++)
			{
				int k = j + i*4;
				position = CGPoint.make((winSize.width/5)*(j+1), (winSize.height/3f)*(i+1)+winSize.height/6);
				_boxImageCharacters[k] = new BoxImageCharacter(context, position);
				_boxImageCharacters[k].set_controler("PC" + k);
				addChild(_boxImageCharacters[k]);
			}
		}
		_boxImageCharacters[0].set_controler("PLAYER");
		
		CCMenuItemImage playGame =  CCMenuItemImage.item("main-menu/PlayButton.png","main-menu/PlayButton.png", this, "playGame");
		playGame.setScale((winSize.height/8.22f)/playGame.getContentSize().height);
		CCMenu play = CCMenu.menu( playGame);
		playGame.setPosition(0, 0);
		
		play.setPosition(winSize.width/2, 100);
		addChild(play,4);
	}
	
	public void playGame(Object sender)
	{
		for(int i=0;i<8;i++)
		{
			_characters[i] = _boxImageCharacters[i].get_charName();
			_teamIDs[i] = _boxImageCharacters[i].get_teamID();
		}
		
		if(_characters[0].compareTo("")!=0 && checkTeam() && MainMenuLayer._isStageMode == false)
		{
			GameScene gameScene = new GameScene(_context);
			CCDirector.sharedDirector().runWithScene(new CCRotoZoomTransition(1,gameScene));
		}
		if(_characters[0].compareTo("")!=0 && MainMenuLayer._isStageMode == true)
		{
			GameScene gameScene = new GameScene(_context);
	        CCDirector.sharedDirector().runWithScene(new CCRotoZoomTransition(1,gameScene));
		}
	}
	
	public boolean checkTeam()
	{
		for(int i=1;i<8;i++)
		{
			if(_teamIDs[0] != _teamIDs[i])
				return true;
		}
		return false;
	}

	public static String[] get_characters() {
		return _characters;
	}

	public static int[] get_teamIDs() {
		return _teamIDs;
	}
}
